package com.example.loginregister.View

import android.annotation.SuppressLint
import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import com.example.loginregister.R
import com.example.loginregister.databinding.UserInputViewBinding

@SuppressLint("InflateParams")
class UserInputView(context: Context, attrs:AttributeSet):FrameLayout(context, attrs) {

    private val binding:UserInputViewBinding by lazy {
        UserInputViewBinding.inflate(LayoutInflater.from(context))
    }

    init {
        val layoutInflater = LayoutInflater.from(context)
        val view = layoutInflater.inflate(R.layout.user_input_view,null,false)

        val lp = LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        addView(binding.root,lp)

        //解析属性
        val typeArray = context.obtainStyledAttributes(attrs,R.styleable.UserInputView)
        val title = typeArray.getString(R.styleable.UserInputView_title)
        val errorTitle = typeArray.getString(R.styleable.UserInputView_errorTitle)
        val inputTitle = typeArray.getString(R.styleable.UserInputView_inputTitle)
        val passwordType = typeArray.getInteger(R.styleable.UserInputView_inputType,1)
        typeArray.recycle()

        binding.textView.text = title
        binding.editText.hint = inputTitle

        if(passwordType == 2){
            binding.editText.inputType =
                InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
    }




}